using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;

/// <summary>
/// 使用UnityEngine.WWW进行Http请求或者读取streamingasset目录下的资源
/// </summary>
public class WWWResponse
{
    public string url;
    public UnityWebRequest www;
    public string error;
    public bool IsSuccess;
    public void Dispose()
    {
        if (www != null)
        {
            www.Dispose();
            www = null;
        }
    }
}

public class WWWRequest
{
    public string url;
    public float progress = 0f;
    public float timeout = 5f;
    public float time = 0f;
    public Action<WWWResponse> OnComplete;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="path">htttp url或者streamingasset路径</param>
    /// <param name="requestCallBack">请求回调</param>
    public static void Get(string path, Action<WWWResponse> requestCallBack)
    {
        WWWRequest request = new WWWRequest();
        request.url = path;
        request.OnComplete = requestCallBack;
        CoroutineProvider.instance.StartCoroutine(request.RequestAsync());
    }

    IEnumerator RequestAsync()
    {
        Debug.Log("Download:" + this.url);
        //Debug.Log("Download:" + this.url);
        UnityWebRequest www = UnityWebRequest.Get(this.url);
        UnityWebRequestAsyncOperation uwaop = www.SendWebRequest();
        while(!uwaop.isDone)
        {
            if (progress == www.downloadProgress)
            {
                time += Time.unscaledDeltaTime;
                if (time > timeout)
                {
                    WWWResponse response = new WWWResponse();
                    response.IsSuccess = false;
                    response.error = "timeout";
                    response.url = this.url;
                    this.OnComplete(response);
                    response.Dispose();
                    www.Dispose();
                    yield break;
                }
                yield return null;
            }
            else
            {
                progress = www.downloadProgress;
                time = 0;
                yield return null;
            }
            //Debug.LogError(this.url + "\nProgress:" + progress);
        }

        if (!www.isDone || !string.IsNullOrEmpty(www.error))
        {
            if (this.OnComplete != null)
            {
                WWWResponse response = new WWWResponse();
                response.IsSuccess = false;
                response.error = www.error;
                response.url = this.url;
                this.OnComplete(response);
                response.Dispose();
            }
        }
        else
        {
            if (this.OnComplete != null)
            {
                WWWResponse response = new WWWResponse();
                response.www = www;
                response.IsSuccess = true;
                response.url = this.url;
                this.OnComplete(response);
                response.Dispose();
            }
        }

        
        www.Dispose();
        www = null;
    }
}